package com.zlfund.cat.zjgsmk.ui.channel;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.zlfund.cat.zjgsmk.R;
import com.zlfund.cat.zjgsmk.api.ZjgsmkService;
import com.zlfund.cat.zjgsmk.constant.Constants;
import com.zlfund.cat.zjgsmk.net.RetrofitFactory;
import com.zlfund.cat.zjgsmk.ui.base.BaseActivity;
import com.zlfund.cat.zjgsmk.ui.user.UserManager;
import com.zlfund.cat.zjgsmk.util.FileUtil;

import java.io.File;
import java.io.IOException;

import butterknife.BindView;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class NewDynamicActivity extends BaseActivity {

    @BindView(R.id.et_title)
    EditText mEtTitle;
    @BindView(R.id.et_content)
    EditText mEtContent;
    @BindView(R.id.btn_publish)
    Button mBtnPublish;
    @BindView(R.id.btn_album)
    Button mBtnAlbum;

    private boolean mCanWriteSDCard;
    private boolean mCanReadSDCard;

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, Constants.WRITE_EXTERNAL_PERMISSION);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == Constants.WRITE_EXTERNAL_PERMISSION) {
            mCanReadSDCard = grantResults[1] == PackageManager.PERMISSION_GRANTED;
            mCanWriteSDCard = grantResults[0] == PackageManager.PERMISSION_GRANTED;
        }
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_new_dynamic);
    }

    @Override
    protected void setListener() {
        mBtnAlbum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mContext, AlbumActivity.class));
            }
        });
    }

    @Override
    protected void initData() {
        File file = Environment.getExternalStorageDirectory();
        File[] files = file.listFiles();
        mBtnPublish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Retrofit retrofit = RetrofitFactory.createRetrofit();
                File file = FileUtil.findFile(".png");
                ZjgsmkService.NewDynamicService service = retrofit.create(ZjgsmkService.NewDynamicService.class);
                RequestBody requestFile =
                        RequestBody.create(MediaType.parse("multipart/form-data"), file);

                // MultipartBody.Part is used to send also the actual file name
                MultipartBody.Part body =
                        MultipartBody.Part.createFormData("file", file.getName(), requestFile);

                // add another part within the multipart request
                String title = "the time seems very good";
                String label = "港城爆料";
                String content = "just for fun,and tody";
                String auth = UserManager.getAuth();
                RequestBody titleBody = RequestBody.create(MediaType.parse("text/plain"), title);
                RequestBody labelBody = RequestBody.create(MediaType.parse("text/plain"), label);
                RequestBody contentBody = RequestBody.create(MediaType.parse("text/plain"), content);
                RequestBody authBody = RequestBody.create(MediaType.parse("text/plain"), auth);
                Call<ResponseBody> call = service.upload(body, titleBody, authBody, labelBody, contentBody);
                call.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        try {
                            String string = response.body().string();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                        String message = t.getMessage();
                    }
                });
            }
        });
    }
}
